package com.yupi.yuapiclientsdk.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.yuapiclientsdk.model.Picture;
import com.yupi.yuapiclientsdk.model.User;


import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import static com.yupi.yuapiclientsdk.utils.SignUtils.genSign;

/**
 * 调用第三方接口的客户端
 *
 * @author <a href=""https://github.com/Fzs0>逢左使</a>
 * @create 2023/12/18 14:25
 * @Description:
 */
public class YuApiClient {

    private static final String GATEWAY_HOST = "http://localhost:8090";

    private String accessKey;

    private String secretKey;

    public YuApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public String getNameByGet(String name) {
        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result = HttpUtil.get(GATEWAY_HOST + "/api/name/", paramMap);
        System.out.println(result);
        return result;
    }

    public String getNameByPost(String name) {
        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result = HttpUtil.post(GATEWAY_HOST + "/api/name/", paramMap);
        System.out.println(result);
        return result;
    }

    private Map<String, String> getHeaderMap(String body) {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("accessKey", accessKey);
        // 一定不能直接发送
//        hashMap.put("secretKey", secretKey);
        hashMap.put("nonce", RandomUtil.randomNumbers(4));
//        hashMap.put("body", body);
        String encodedBody = Base64.getEncoder().encodeToString(body.getBytes(StandardCharsets.UTF_8));
        hashMap.put("body", encodedBody);

        hashMap.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        // sk
        hashMap.put("sign", genSign(body, secretKey));
        return hashMap;
    }

    /**
     * 无请求体 get请求
     * @return
     */
    private Map<String, String> getHeaderMap() {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("accessKey", accessKey);
        // 一定不能直接发送
//        hashMap.put("secretKey", secretKey);
        hashMap.put("nonce", RandomUtil.randomNumbers(4));
//        hashMap.put("body", body);
//        String encodedBody = Base64.getEncoder().encodeToString(body.getBytes(StandardCharsets.UTF_8));
//        hashMap.put("body", encodedBody);

        hashMap.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        // sk
        hashMap.put("sign", genSign(secretKey));
        return hashMap;
    }

    public String getUsernameByPost(User user) {
        String json = JSONUtil.toJsonStr(user);
        // 把要验证的信息放入其中 经过网关过滤
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/name/user")
                .addHeaders(getHeaderMap(json))
                .body(json,"application/json")
                .execute();
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }

    /**
     * 获得一张图片的url
     * @param picture
     * @return
     */
    public String getPictureByPost(Picture picture) {
        String json = JSONUtil.toJsonStr(picture);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/picture/getOne")
                .addHeaders(getHeaderMap(json))
                .body(json,"application/json")
                .execute();
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }

    /**
     * 随机获得一张图片 无需参数
     * @param
     * @return
     */
    public String getImageByRandom() {
        // 要调用的接口 /image/random
        HttpResponse httpResponse = HttpRequest.get(GATEWAY_HOST + "/api/image/random")
                .addHeaders(getHeaderMap())
                .execute();
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }

}
